import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		State: 0, //状态
		ComparisonArr1: {}, //对比数据一
		ComparisonArr2: {}, //对比数据一
		ComparisonState: false, //对比状态
		NutritionArr: []
	},
	// 方法
	mutations: {
		toggleState: function(state, newval) {
			state.State = newval;
		},
		toggleComparisonState: function(state, newval) {
			state.ComparisonState = newval;
		},
		toggleComparisonArr: function(state, newval) {
			if (state.ComparisonArr1.name) {
				state.ComparisonArr2 = newval;
				if (state.ComparisonArr1.nutrition.length >= state.ComparisonArr2.nutrition.length) {
					state.NutritionArr = state.ComparisonArr1.nutrition.map((item) => {
						return item.name
					});
				}else{
					state.NutritionArr = state.ComparisonArr2.nutrition.map((item) => {
						return item.name
					});
				}
			} else {
				state.ComparisonArr1 = newval;
				state.NutritionArr = state.ComparisonArr1.nutrition.map((item) => {
					return item.name
				});
			}
		},
		deteleComparisonArr: function(state, num) {
			if (num == 1) {
				state.ComparisonArr1 = {}
			} else if (num == 2) {
				state.ComparisonArr2 = {}
			}
		}
	},
})
export default store
